﻿using BaseDll;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.ComponentModel;

namespace OtherDevice
{
    [Description("基恩士扫码器-SR-X")]
    public class Scan_SR_X : SNScan_Base
    {
        public delegate void EventSNScanDelegate(string strSn);
        public event EventSNScanDelegate EventGetSn = null;
        private List<string> list_SN = new List<string>();
        private object obj = new object();
        public Scan_SR_X(string strName) : base(strName)
        {
            controlFrom = new Form_扫码();
        }
        int n_Length = 0;
        public override void InitDeal()
        {
            m_SerialPort.DataReceived += M_SerialPort_DataReceived;
        }
        private void M_SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            string strRecivSN = "";
            byte[] byte_Result = new byte[n_Length];
            ReceiveData(ref byte_Result, 6000);
            strRecivSN = Encoding.Default.GetString(byte_Result);
            strRecivSN = strRecivSN.Replace("\r", "");
            list_SN.Add(strRecivSN);
            if (EventGetSn != null)
                EventGetSn(strRecivSN);
        }

        public override bool Function_触发扫码(int n_SnLength, ref string strSN, int n_Timeout = 2000)
        {
            try
            {
                lock (obj)
                {
                    n_Length = n_SnLength;
                    list_SN.Clear();
                    int n_RetryTime = 0;
                Retry:
                    SendData("LON\r");
                    Stopwatch watch = new Stopwatch();
                    watch.Start();
                    while (true)
                    {
                        if (watch.ElapsedMilliseconds > n_Timeout)
                        {
                            n_RetryTime++;
                            if (n_RetryTime > 3)
                            {
                                SendData("LOFF\r");
                                return false;
                            }
                            else
                                goto Retry;
                        }
                        if (list_SN.Count >= 1)
                        {
                            strSN = list_SN[0];
                            return true;
                        }
                        Thread.Sleep(5);

                    }

                }

            }
            catch(Exception ex)
            {
                System.Windows.Forms.MessageBox.Show($"数据获取失败{ex}");
                return false;
            }

        }

        public override int Fucntion_获取SN码(int n_SnLength, ref string strSN,int n_Timeout=2000)
        {
            lock (obj)
            {
                n_Length = n_SnLength;
                list_SN.Clear();
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                while (true)
                {
                    if(n_Timeout>0&& stopwatch.ElapsedMilliseconds > n_Timeout)
                    {
                           strSN = "None";
                            return -2;
                    }
                    if (list_SN.Count >= 1)
                    {                        
                        strSN = list_SN[0];
                        if (strSN.Contains("ER,LON"))
                            return -1;
                        else
                            return 0;
                    }
                    Thread.Sleep(5);
                }
               

            }
        }
    }
}